/*
 * ********************************************************************************
 * Project:        GAMES, 7th Framework Programme, co-funded by European Commission
 * Product:        ESMI Prototype
 * File:           DatePopupInputPrompt.java (UTF-8)
 * Created by:     Alessandro Rossi <alessandro.rossi@eng.it>
 * Created on:     13-dic-2011 2.44.37
 * 
 * (C) Copyright 2010-2012 Engineering Ingegneria Informatica S.p.A.
 * 
 * This file is part of the Energy Sensing & Monitoring Infrastructure of GAMES.
 * ********************************************************************************
 * 
 * The terms of the license are under discussion and will be defined.
 * Until then, the distribution of this file is restricted to the members of the GAMES Consortium.
 */
package eu.games.esmi.ieat.indicatorrelations.ui;

import java.text.DateFormat;
import java.util.Date;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PopupDateField;

/**
 *
 * @author Alessandro
 */
@SuppressWarnings("serial")
public class DatePopupInputPrompt extends HorizontalLayout implements
        ValueChangeListener {

    private PopupDateField datetime;
    
    public Date getDatetime() {
        Object value = datetime.getValue();
        if (value == null || !(value instanceof Date))
            return null;
        else
            return (Date) value;
    }

    public DatePopupInputPrompt(String caption) {
        this(caption, null);
    }
    
    public DatePopupInputPrompt(String caption, Date initialValue) {
        setSpacing(true);

        datetime = new PopupDateField(caption);
        datetime.setInputPrompt("Set timestamp");

        if (initialValue != null)
            datetime.setValue(initialValue);
        datetime.setImmediate(true);
        datetime.setShowISOWeekNumbers(true);
        // Set the correct resolution
        datetime.setResolution(PopupDateField.RESOLUTION_SEC);

        // Add valuechangelistener
        datetime.addListener(this);
        datetime.setImmediate(true);

        addComponent(datetime);
    }

    public void valueChange(ValueChangeEvent event) {
        // Get the new value and format it to the current locale
        Object value = event.getProperty().getValue();
        if (value == null || !(value instanceof Date)) {
            getWindow().showNotification("Invalid timestamp entered");
//        } else {
//            DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.SHORT);
//            String dateOut = dateFormatter.format(value);
//            // Show notification
//            getWindow().showNotification("Timestamp set to: " + dateOut);
        }
    }
}
